package com.vortex.cloud.zhsw.xinyang.util;

import cn.hutool.core.util.StrUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AddressUtils {
    public static Map<String, String> parseAddress(String address) {
        Map<String, String> result = new HashMap<>();
        if (StrUtil.isEmpty(address)){
            return result;
        }
        try {
            // 正则表达式匹配区
            Pattern districtPattern = Pattern.compile("(?<=(市|县|省))[^市县省]+?(?=区|县)");
            Matcher districtMatcher = districtPattern.matcher(address);
            if (districtMatcher.find()) {
                result.put("district", districtMatcher.group() + "区");
            } else {
                result.put("district", null);
            }
            // 正则表达式匹配街道/镇
            Pattern streetPattern = Pattern.compile("(?<=区|县)[^区县]+?(?=街道|镇)");
            Matcher streetMatcher = streetPattern.matcher(address);
            if (streetMatcher.find()) {
                String street = streetMatcher.group() + (address.contains("街道") ? "街道" : "镇");
                result.put("street", street.contains("中国共产党") ? street.replace("中国共产党","") : street);
            } else {
                result.put("street", null);
            }
        }catch (Exception e){

        }
        return result;
    }
}
